<template>
  <el-dialog title="添加或修改新单词" :visible.sync="visible" :show-close="false" width="480px" center>
    <div class="previewWord">
      <knowledge-item-div type="word" :content="previewWord" size-type="small"></knowledge-item-div>
    </div>
    <el-form :model="data" class="form-class" :rules="rules" ref="wordForm">
      <el-form-item label="单词" :label-width="labelWidth" prop="content">
        <el-input v-model="data.content" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="语调" :label-width="labelWidth" prop="pitch">
        <el-select v-model="data.pitch" filterable placeholder="请选择" style="width: 240px; font-size: 56px;">
          <el-option v-for="item in pitchOptions" :key="item" :label="item" :value="item"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="词性" :label-width="labelWidth" prop="partOfSpeech">
        <el-select v-model="data.partOfSpeech" filterable placeholder="请选择" style="width: 240px; font-size: 56px;">
          <el-option v-for="item in partOfSpeechOptions" :key="item" :label="item" :value="item"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="中文意思" :label-width="labelWidth" prop="meaning">
        <el-input v-model="data.meaning" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="分类" :label-width="labelWidth" prop="category">
        <el-select v-model="data.category" filterable placeholder="请选择" style="width: 240px; font-size: 56px;">
          <el-option v-for="item in categoryOptions" :key="item" :label="item" :value="item"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否特殊" :label-width="labelWidth">
        <el-switch v-model="data.special"></el-switch>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="submit" v-loading.fullscreen.lock="fullscreenLoading">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import KnowledgeItemDiv from "@/components/KnowledgeItemDiv";
import {combinedDisplayWord} from '@/utils/stringUtil'
import {saveOrModifyEditedWord} from "@/apis/sentence.api";
export default {
  name: "AddOrModifyWordDialog",
  components: {
    KnowledgeItemDiv
  },
  props: {
    visible: Boolean
  },
  data() {
    return {
      pitchOptions: ['⓪','①','②','③','④','⑤','^'],
      partOfSpeechOptions: ['v','n','adv','adj','prep'],
      categoryOptions: ['Select','1类','2类','3类','い形容词','な形容词'],
      labelWidth: '108px',
      fullscreenLoading: false,
      rules: {
        content: [
          { required: true, message: '请填写单词', trigger: 'blur' }
        ],
        pitch: [
          { required: true, message: '请选择一个语调', trigger: 'change' }
        ],
        partOfSpeech: [
          { required: true, message: '请选择一个词性', trigger: 'change'}
        ],
        meaning: [
          { required: true, message: '请填写中文意思', trigger: 'blur' }
        ],
        category: [
          { required: true, message: '请选择一个分类', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    data() {
      return this.$store.getters.editedWord;
    },
    editedSentenceId() {
      return this.$store.getters.editedSentenceId;
    },
    previewWord() {
      return combinedDisplayWord(this.data);
    }
  },
  methods: {
    updateVisible(val) {
      this.$emit('update:visible', val);
    },
    updateEditedWord(payload) {
      this.$store.commit("updateEditedWord", payload);
    },
    clearForm() {
      this.updateEditedWord({
        content: '',
        pitch: '',
        partOfSpeech: '',
        meaning: '',
        category: '',
        special: false
      });
      this.$refs.wordForm.resetFields();
    },
    cancel() {
      this.updateVisible(false);
      this.clearForm();
    },
    async saveOrModifyItem() {
      if (this.data.category === 'Select') {
        this.data.category = null
      }
      return await saveOrModifyEditedWord(this.editedSentenceId.videoNum, this.editedSentenceId.seq, this.data);
    },
    showSaveResult(res) {
      if (res.data === 'success') {
        this.$message({type: 'success', message: '添加成功!'});
      } else {
        this.$message({type: 'error', message: '添加失敗!'});
      }
    },
    submit() {
      this.$refs.wordForm.validate(async (valid) => {
        if (valid) {
          this.fullscreenLoading = true;
          let res = await this.saveOrModifyItem();
          this.showSaveResult(res);
          this.updateVisible(false);
          this.clearForm();
          this.fullscreenLoading = false;
          location.reload();
        } else {
          return false;
        }
      });
    }
  },
  watch: {
    data() {
      this.updateEditedWord(this.data);
    }
  }
}
</script>

<style scoped>
@import "../assets/scss/dialog.scss";
@import "../assets/scss/form.scss";

.previewWord {
  margin-left: 63px;
  margin-bottom: 50px;
}
</style>